package com.cyandpos.pad.offline.offlinedb;

import android.database.Cursor;
import android.util.Base64;
import android.util.Log;

import com.cyandpos.pad.offline.bean.OfflineBean;
import com.cyandpos.pad.utils.ComSharedPreferences;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class CursorUtil {

    public static String getString( Cursor cur,String key){
        try {
        String str=cur.getString(cur.getColumnIndex(key));

        if(str!=null){
            return  str;
        }
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }

    public static int getInt( Cursor cur,String key){
        try {
            int str=cur.getInt(cur.getColumnIndex(key));
            if(!"null".equals(str+"")){
                return  str;
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return 0;
    }

    public static double getDouble( Cursor cur,String key){
        try {
        double str=cur.getDouble(cur.getColumnIndex(key));
        if(!"null".equals(str+"")){
            return  str;
        }
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0.0;
    }


  public static JsonObject  getOfflineBean(Object o){
      OfflineBean offlineBean=new OfflineBean();
      offlineBean.setCode("0");
      offlineBean.setData(o);
        JsonObject response=new JsonParser().parse(new Gson().toJson(offlineBean)).getAsJsonObject();
        return response;
  }

    public static int getJLBH(){
        int jlbh=0;
        jlbh= Integer.parseInt(ComSharedPreferences.getValueByName("JLBH","jlbh",0,"0")+"")+1;
        return jlbh;
    }
    public static int getNextval(){
        int nextval= Integer.parseInt(ComSharedPreferences.getValueByName("NEXTVAL","nextval",1,"0")+"")+1;
        ComSharedPreferences.WriteSharedPreferences("NEXTVAL", "nextval", nextval);
        return nextval;
    }
    public static String getMyUUID(){
        UUID uuid = UUID.randomUUID();

        String uniqueId = uuid.toString();

        Log.d("debug","----->UUID"+uuid);

        return uniqueId;

    }

    //base64字符串转byte[]
    public static byte[] base64String2ByteFun(String base64Str){
        try {
            return Base64.decode(base64Str.getBytes("GBK"),Base64.DEFAULT);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
    //byte[]转base64
    public static String byte2Base64StringFun(byte[] b){
        return Base64.encodeToString(b,Base64.DEFAULT);
    }


    /**
     * @Description: GZIP 数据解压缩
     * @author (ljh) @date 2015-4-13 下午6:00:42
     * @param data
     * @return
     * @throws IOException
     * @return byte[]
     */
    public static byte[] uncompress(byte[] data) throws IOException {

        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        uncompress(bais, baos);
        bais.close();
        return baos.toByteArray();
    }

    /**
     * @Description: GZIP 数据解压缩
     * @author (ljh) @date 2015-4-14 上午9:26:51
     * @param is
     * @param os
     * @throws IOException
     * @return void
     */
    private static void uncompress(InputStream is, OutputStream os) throws IOException {
        BZip2CompressorInputStream bzip2IS = new BZip2CompressorInputStream(is);
        int count;
        byte data[] = new byte[2048];
        while ((count = bzip2IS.read(data, 0, data.length)) != -1) {
            os.write(data, 0, count);
        }
        os.flush();
        os.close();
        bzip2IS.close();
    }


    /**
     * 数据压缩
     *
     * @param data
     * @return
     * @throws Exception
     */
    public static byte[] compress(byte[] data) throws Exception {
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        // 压缩
        compress(bais, baos);

        byte[] output = baos.toByteArray();

        baos.flush();
        baos.close();

        bais.close();

        return output;
    }


    /**
     * 数据压缩
     * @param is
     * @param os
     * @throws Exception
     */
    public static void compress(InputStream is, OutputStream os)
            throws Exception {

        BZip2CompressorOutputStream gos = new BZip2CompressorOutputStream(os);

        int count;
        byte data[] = new byte[2048];
        while ((count = is.read(data, 0, 2048)) != -1) {
            gos.write(data, 0, count);
        }

        gos.finish();
        gos.flush();
        gos.close();
    }


    /**
     * 把zip文件解压到指定的文件夹
     * @param zipFile   zip文件路径, 如 "D:/test/aa.zip"
     * @param targetDir   解压后的文件存放路径, 如"D:/test/"
     */
    public static void Unzip(String zipFile, String targetDir) {
        int BUFFER = 4096; //这里缓冲区我们使用4KB，
        String strEntry; //保存每个zip的条目名称

        try {
            BufferedOutputStream dest = null; //缓冲输出流
            FileInputStream fis = new FileInputStream(zipFile);
            ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
            ZipEntry entry; //每个zip条目的实例

            while ((entry = zis.getNextEntry()) != null) {
                try {
                    Log.i("Unzip: ","="+ entry);
                    int count;
                    byte data[] = new byte[BUFFER];
                    strEntry = entry.getName();

                    File entryFile = new File(targetDir +"/"+ strEntry);
                    File entryDir = new File(entryFile.getParent());
                    if (!entryDir.exists()) {
                        entryDir.mkdirs();
                    }

                    FileOutputStream fos = new FileOutputStream(entryFile);
                    dest = new BufferedOutputStream(fos, BUFFER);
                    while ((count = zis.read(data, 0, BUFFER)) != -1) {
                        dest.write(data, 0, count);
                    }
                    dest.flush();
                    dest.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            zis.close();
        } catch (Exception cwj) {
            cwj.printStackTrace();
        }
    }

}
